package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.conmon.enums.SystemCode;
import com.woniuxy.mapper.GoodsMapper;
import com.woniuxy.mapper.OrderMapper;
import com.woniuxy.mapper.UserAddressesMapper;
import com.woniuxy.pojo.Goods;
import com.woniuxy.pojo.Order;
import com.woniuxy.pojo.UserAddresses;
import com.woniuxy.pojo.dto.MergeOrdersDTO;
import com.woniuxy.pojo.dto.OrderDTO;
import com.woniuxy.pojo.vo.OrderDetailVO;
import com.woniuxy.pojo.vo.OrderVO;
import com.woniuxy.pojo.vo.StatusCountVO;
import com.woniuxy.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* @author 29331
* @description 针对表【t_order(订单管理表)】的数据库操作Service实现
* @createDate 2025-06-04 16:14:26
*/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
    implements OrderService {
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    UserAddressesMapper userAddressesMapper;

    @Override
    public Page<Order> getAllOrder(Integer pageSize, Integer pageNum) {
        Page<Order> page = new Page<>(pageNum,pageSize,true);
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Order::getOrderTime);

        return orderMapper.selectPage(page,wrapper);
    }

    @Override
    public List<Order> getOrderByStatus(Integer orderStatus) {
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Order::getOrderStatus,orderStatus).orderByDesc(Order::getOrderTime);

        return orderMapper.selectList(wrapper);
    }

    @Override
    public List<Integer> getOrderStatus() {
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Order::getOrderStatus).groupBy(Order::getOrderStatus)
                .orderByAsc(Order::getOrderStatus);
        List<Order> orders = orderMapper.selectList(wrapper);
        List<Integer> status = new ArrayList<>();
        for (Order order: orders){
            status.add(order.getOrderStatus());
        }
        return status;
    }

    @Override
    public List<String> getOrderSource() {
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Order::getOrderSource).groupBy(Order::getOrderSource);
        List<Order> orders = orderMapper.selectList(wrapper);
        List<String> status = new ArrayList<>();
        for (Order order: orders){
            status.add(order.getOrderSource());
        }
        return status;
    }

    @Override
    public List<Order> selectOrders(OrderDTO orderDTO) {
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        if (orderDTO.getOrderNumber() != null && !orderDTO.getOrderNumber().isEmpty()){
            wrapper.like(Order::getOrderNumber,orderDTO.getOrderNumber());
        }

        if (orderDTO.getOrderSource() !=null && !orderDTO.getOrderSource().isEmpty()){
            wrapper.eq(Order::getOrderSource,orderDTO.getOrderSource());
        }

        if (orderDTO.getOrderStatus()!=null){
            wrapper.eq(Order::getOrderStatus,orderDTO.getOrderStatus());
        }

        if (orderDTO.getUserAccount()!=null && !orderDTO.getUserAccount().isEmpty()){
            wrapper.like(Order::getUserAccount,orderDTO.getUserAccount());
        }
        if (orderDTO.getStartDate() != null && orderDTO.getEndDate() != null) {
            wrapper.between(Order::getOrderTime, orderDTO.getStartDate(), orderDTO.getEndDate());
        }
        return orderMapper.selectList(wrapper);
    }

    @Override
    public List<Order> getOrderByIds(List<Long> ids) {

        return  orderMapper.selectBatchIds(ids);
    }

    @Override
    public StatusCountVO getStatusCounts() {
        StatusCountVO vo = new StatusCountVO();
        vo.setTotal(orderMapper.getTotalCount());
        vo.setWaitPay(orderMapper.getStatusCount(1));
        vo.setWaitShip(orderMapper.getStatusCount(2));
        vo.setShipped(orderMapper.getStatusCount(3));
        vo.setCompleted(orderMapper.getStatusCount(4));
        vo.setClosed(orderMapper.getStatusCount(5));
        return vo;
    }

    @Override
    public void deleteOrder(Long id, String description) {
        Order order = orderMapper.selectById(id);
        order.setDeleted(1);
        order.setDescription(description);
        orderMapper.updateById(order);
    }

    @Override
    @Transactional
    public ResponseEntity<?> mergeOrders(MergeOrdersDTO mergeOrdersDTO) {
        Long mainOrderId = mergeOrdersDTO.getMainOrderId();
        Order order = orderMapper.selectById(mainOrderId);
        List<Long> list = mergeOrdersDTO.getSubOrderIds();
        for(Long o : list){
            Order subOrder = orderMapper.selectById(o);
            if (order.getOrderAmount()!=subOrder.getOrderAmount()){
                subOrder.setOrderNumber(order.getOrderNumber());
                subOrder.setPaymentMethod(order.getPaymentMethod());
                subOrder.setOrderSource(order.getOrderSource());
                orderMapper.updateById(subOrder);
            }else {

                throw new RuntimeException("子订单"+ subOrder + "与主订单下单账号不同");

            }

        }

        return ResponseEntity.ok(SystemCode.MERGEORDERS_SUCCESS.getMessage());

    }

    @Override
    public OrderDetailVO getOrderDetail(Long id) {
        Order order = orderMapper.selectById(id);
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Order::getOrderNumber,order.getOrderNumber());
        List<OrderVO> goodsList = new ArrayList<>();
        List<Order> orders = orderMapper.selectList(wrapper);
        for (Order o : orders){
            OrderVO orderVO = new OrderVO();

            Goods goods = goodsMapper.selectByName(o.getProductName());
            orderVO.setGoodsNum(goods.getGoodsNum());
            orderVO.setImage(goods.getImage());
            orderVO.setName(goods.getName());
            orderVO.setNumbers(o.getNumbers());
            orderVO.setPrice(goods.getPrice());

            goodsList.add(orderVO);
        }
        UserAddresses userAddresses = userAddressesMapper.selectById(order.getAddressId());
        OrderDetailVO orderDetailVO = new OrderDetailVO();
        orderDetailVO.setOrderNumber(order.getOrderNumber());
        orderDetailVO.setOrderSource(order.getOrderSource());
        orderDetailVO.setFee(order.getFee());
        orderDetailVO.setUserAccount(order.getUserAccount());
        orderDetailVO.setPaymentMethod(order.getPaymentMethod());
        orderDetailVO.setId(order.getId());
        orderDetailVO.setGoods(goodsList);
        orderDetailVO.setUserAddresses(userAddresses);

        return orderDetailVO;
    }
}




